package com.bida.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.ordering.ResourceListingPositionalOrdering;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

/**
 * Configuration for Swagger.
 * 
 * @author chenli
 *
 */
@Configuration
@EnableSwagger
public class SwaggerPluginConfig {

    /**
     * Auto injected SpringSwaggerConfig.
     */
    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig.
     * 
     * @param springSwaggerConfig
     *            SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * Register SpringSwaggerConfig bean to Spring container.
     * 
     * @return SpringSwaggerConfig
     */
    @Bean
    public SpringSwaggerConfig springSwaggerConfig() {
        return new SpringSwaggerConfig();
    }

    /**
     * Register bean for SwaggerSpringMvcPlugin.
     * 
     * @return SwaggerSpringMvcPlugin
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        SwaggerSpringMvcPlugin swaggerSpringMvcPlugin = new SwaggerSpringMvcPlugin(this.springSwaggerConfig);
        swaggerSpringMvcPlugin.apiListingReferenceOrdering(new ResourceListingPositionalOrdering());
        swaggerSpringMvcPlugin.apiInfo(apiInfo());
        swaggerSpringMvcPlugin.includePatterns("^/rest.*", "^/file.*", "^/api.*");
        return swaggerSpringMvcPlugin;
    }

    /**
     * Create API info.
     * 
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("PCS APIs v2", "Next-gen of bida platform services", "", "", "", "");
        return apiInfo;
    }
}
